package com.lxd.sunso.product.service.impl;

import com.lxd.sunso.common.utils.PageUtils;
import com.lxd.sunso.common.utils.Query;
import com.lxd.sunso.product.components.OSSClientUtil;
import com.lxd.sunso.product.entity.CustomerEntity;
import com.lxd.sunso.product.entity.MachineEntity;
import com.lxd.sunso.product.service.CustomerService;
import com.lxd.sunso.product.service.MachineService;
import com.lxd.sunso.product.vo.CustomerVo;
import com.lxd.sunso.product.vo.MachineKindVo;
import com.lxd.sunso.product.vo.MachineNameAndIdVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxd.sunso.product.dao.MachinekindDao;
import com.lxd.sunso.product.entity.MachinekindEntity;
import com.lxd.sunso.product.service.MachinekindService;
import org.springframework.web.multipart.MultipartFile;


@Service("machinekindService")
public class MachinekindServiceImpl extends ServiceImpl<MachinekindDao, MachinekindEntity> implements MachinekindService {

    @Autowired
    private MachineService machineService;

    @Autowired
    private CustomerService customerService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MachinekindEntity> page = this.page(
                new Query<MachinekindEntity>().getPage(params),
                new QueryWrapper<MachinekindEntity>()
        );
        List<MachinekindEntity> machinekindEntities = page.getRecords();
        List<MachinekindEntity> collect = machinekindEntities.stream().map(entity -> {
            String machineStatu = getMachineStatu(entity.getId());
            entity.setNum(machineStatu);
            return entity;
        }).collect(Collectors.toList());
        page.setRecords(collect);
        return new PageUtils(page);
    }

    @Override
    public List<MachinekindEntity> search(Long id) {
        String machineStatu = getMachineStatu(id);
        List<MachinekindEntity> list = this.list(new QueryWrapper<MachinekindEntity>().eq("ID", id));
        List<MachinekindEntity> collect = list.stream().map(entity -> {
            entity.setNum(machineStatu);
            return entity;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public void updateKind(MachinekindEntity machinekind, MultipartFile file) {
        if (file != null) {
            String imgurl = updateHead(file);
            machinekind.setMachineimg(imgurl);
            Long kindId = machinekind.getId();
            MachineEntity machineEntity = new MachineEntity();
            machineEntity.setImgurl(imgurl);
            machineService.update(machineEntity, new QueryWrapper<MachineEntity>().eq("MachineKind", kindId));
        }
        this.updateById(machinekind);
    }

    @Override
    public void saveKind(MachinekindEntity machinekind, MultipartFile file) {
        if (file != null) {
            String imgurl = updateHead(file);
            //System.out.println(imgurl);
            machinekind.setMachineimg(imgurl);
        } else {
            machinekind.setMachineimg("https://sunso.oss-cn-shenzhen.aliyuncs.com/machine/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20210112091932.png");
        }
        this.save(machinekind);
    }

    @Override
    public void delete(List<Long> asList) {
        for (Long machineKindId : asList) {
            List<MachineEntity> entities = machineService.list(new QueryWrapper<MachineEntity>().eq("MachineKind", machineKindId));
            if (entities != null && entities.size() > 0) {
                List<Long> machineIds = entities.stream().map(entity -> {
                    return entity.getId();
                }).collect(Collectors.toList());
                if (machineIds != null && machineIds.size() > 0) {
                    machineService.delete(machineIds);
                }
            }
            this.removeByIds(asList);
        }
    }

    //获取所有的型号名称和ID
    @Override
    public List<MachineKindVo> getMachineKindAndId() {
        List<MachinekindEntity> list = this.list();
        List<MachineKindVo> collect = list.stream().map(entity -> {
            MachineKindVo machineKindVo = new MachineKindVo();
            BeanUtils.copyProperties(entity, machineKindVo);
            return machineKindVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<MachineNameAndIdVo> getMahicneByKindName(String machineKind) {
        MachinekindEntity entity = this.getOne(new QueryWrapper<MachinekindEntity>().eq("MachineNumber", machineKind));
        List<MachineEntity> list = machineService.list(new QueryWrapper<MachineEntity>().eq("MachineKind", entity.getId()));
        List<MachineNameAndIdVo> collect = list.stream().map(item -> {
            MachineNameAndIdVo machineNameAndIdVo = new MachineNameAndIdVo();
            BeanUtils.copyProperties(item, machineNameAndIdVo);
            return machineNameAndIdVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<CustomerVo> getCustomerByKindName(String machineKind) {
        MachinekindEntity entity = this.getOne(new QueryWrapper<MachinekindEntity>().eq("MachineNumber", machineKind));
        List<MachineEntity> list = machineService.list(new QueryWrapper<MachineEntity>().eq("MachineKind", entity.getId()));
        List<Long> customerIds = list.stream().map(temp -> {
            return temp.getCustomerid();
        }).collect(Collectors.toList());
        Collection<CustomerEntity> customerEntities = customerService.listByIds(customerIds);
        List<CustomerVo> collect = customerEntities.stream().map(item -> {
            CustomerVo customerVo = new CustomerVo();
            BeanUtils.copyProperties(item, customerVo);
            return customerVo;
        }).collect(Collectors.toList());
        return collect;
    }

    private String getMachineStatu(Long id) {
        Long onNum = machineService.queryOnByMachineKind(id);
        Long num = machineService.queryNumByMachineKind(id);
        return (onNum + "/" + num);
    }

    public String updateHead(MultipartFile file) {
        if (file == null || file.getSize() <= 0) {
            throw new RuntimeException("file不能为空");
        }
        OSSClientUtil ossClient = new OSSClientUtil();
        String name = null;
        try {
            name = ossClient.uploadImg2Oss(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String imgUrl = ossClient.getImgUrl(name);
        String[] split = imgUrl.split("\\?");
        return split[0];
    }
}